﻿function ShareXml(viewType) {
    sld('xml-form');
    $.post(SerUrl + 'Callback/Viewer.aspx', { type: 1, xml: encodeURIComponent($('#result-text-xml').val()), 'ViewType': viewType }, function (d) {
        MsgBox(d);
    });
}
function ShowXml() {
    $('#xml-viewer').html('');
    d = null;
    ShowAsObject($('#result-text-xml').val());
}
function ShowAsObject(xml) {
    if (IsEmpty(xml)) return;
    d = new dTree('d');
    d.add(0, -1, 'XML Viewer', null);
    var dParser = new DOMParser().parseFromString(xml, 'text/xml');
    if (dParser.childNodes[0].nodeName == "parsererror") dParser = new DOMParser().parseFromString(HtmlEncode(xml), 'text/xml');
    var node = dParser.childNodes[0];
    SetNode(node, 0, d);
    $('#xml-viewer').html('<div style="padding-bottom:5px;"><a href="javascript:d.openAll();" class="a-btn">Expand All Nodes</a><span class="mm-split">|</span><a href="javascript: d.closeAll();" class="a-btn" style="">Close All Nodes</a></div>').append(d.toString());
}
function GetNodeAttributes(node) {
    if (node.attributes == null) return "";
    if (node.attributes.length == 0) return "";
    var attributesDisplay = '';
    for (var i = 0; i < node.attributes.length; i++) {
        if (node.attributes[i].name.indexOf('xmlns') >= 0 || node.attributes[i].name.indexOf('xsi:') >= 0) continue;
        attributesDisplay += ' <span style="color:#FD0034;">' + node.attributes[i].name + '</span><span style="color:#0000FD;">=</span>"<span style="color:#0000FD;">' + node.attributes[i].value + '</span>"';
    }
    return attributesDisplay;
}
function SetNode(node, parentId, ddtree) {
    GetNodeAttributes(node);
    var currentId = ddtree.aNodes.length;
    var nodeNameDisplay = '<span style="color:#A31515;">' + node.nodeName + '</span>';
    var displayName = '<span style="color:#0000FD;">' + HtmlEncode("<") + '</span>' + nodeNameDisplay + GetNodeAttributes(node) + '<span style="color:#0000FD;">' + HtmlEncode(">") + '</span>';
    if (node.hasChildNodes() && node.childElementCount > 0) {
        displayName += " (" + node.childElementCount + ")";
    }
    else {
        displayName += HtmlEncode(node.textContent).replace(/\r\n|\r|\n/g, '<br />') + '<span style="color:#0000FD;">' + HtmlEncode("</") + '</span>' + nodeNameDisplay + '<span style="color:#0000FD;">' + HtmlEncode(">") + '</span>';
    }
    ddtree.add(ddtree.aNodes.length, parentId, displayName);
    if (node.hasChildNodes()) {
        for (var i = 0; i < node.childNodes.length; i++) {
            if (node.childNodes[i].nodeType != 1) continue;
            SetNode(node.childNodes[i], currentId, ddtree);
        }
    }
}